/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.model.Element;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import simplemetrosystem.model.Element.Interface.MetroElement;
import simplemetrosystem.model.Element.Interface.Selectable;
import simplemetrosystem.model.Element.Interface.HighLightable;
import simplemetrosystem.model.Element.Interface.CanvasElement;
import java.awt.Graphics2D;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import simplemetrosystem.control.ElementFactory;

/**
 *
 * @author Eric Qian
 */
public class MetroString implements MetroElement, CanvasElement,
        HighLightable, Selectable {

    /**
     * 
     * @param string 
     * @param element
     * @param logX
     * @param logY
     */
    public MetroString(String string, MetroElement element, int logX, int logY) {
        this.string = string;
        this.element = element;
        this.logX = logX;
        this.logY = logY;
        this.highLightFlag = false;
        this.selectFlag = false;
        this.shadowFlag = false;
        this.shaX = 0;
        this.shaY = 0;
    }

    public String getName() {
        return element != null ? element.getName() : string;
    }

    public String getChineseName() {
        return element != null ? element.getChineseName() : string;
    }

    public String getDisplayName() {
        return element != null ? element.getDisplayName() : string;
    }

    public Element getElement(Document doc) {
        Element stringElement = doc.createElement(ElementFactory.ROOT_STRING);

        if (element != null) {
            Element elemElement = doc.createElement(ElementFactory.STR_ELEM);
            stringElement.appendChild(elemElement);
            Text elemText = doc.createTextNode(element.getName());
            elemElement.appendChild(elemText);
        }

        Element strElement = doc.createElement(ElementFactory.STR_STR);
        stringElement.appendChild(strElement);
        Text strText = doc.createTextNode(string);
        strElement.appendChild(strText);

        Element logXElement = doc.createElement(ElementFactory.STR_LOGX);
        stringElement.appendChild(logXElement);
        Text logXText = doc.createTextNode("" + logX);
        logXElement.appendChild(logXText);

        Element logYElement = doc.createElement(ElementFactory.STR_LOGY);
        stringElement.appendChild(logYElement);
        Text logYText = doc.createTextNode("" + logY);
        logYElement.appendChild(logYText);

        return stringElement;
    }

    public int getLogX() {
        return logX;
    }

    public int getLogY() {
        return logY;
    }

    /**
     * 
     * @return
     */
    public int getHeight() {
        return HEIGHT;
    }

    public void setLogX(int logX) {
        this.logX = logX;
    }

    public void setLogY(int logY) {
        this.logY = logY;
    }

    public void setString(String string) {
        this.string = string;
    }

    /**
     * 
     * @return
     */
    public int getWidth() {
        return HEIGHT;
    //return element.getDisplayName().length() / 4;
    }

    public void draw(Graphics2D gg, int matrixSize) {
        Graphics2D g2D = (Graphics2D) gg.create();
        g2D.setColor(Color.BLACK);
        g2D.setFont(new Font(null, Font.BOLD, matrixSize * 2));
        g2D.drawString(getDisplayName(), logX * matrixSize, (float) (logY + 1.6) * matrixSize);
    }

    public boolean isHighLight() {
        return highLightFlag;
    }

    public void setHighLight(boolean lightFlag) {
        this.highLightFlag = lightFlag;
    }

    public void drawHighLight(Graphics2D gg, int matrixSize) {
        Graphics2D g2D = (Graphics2D) gg.create();

        g2D.setColor(Color.RED);

        g2D.setFont(new Font(null, Font.BOLD, matrixSize * 2));
        g2D.drawString(getDisplayName(), logX * matrixSize, (float) (logY + 1.6) * matrixSize);
    }

    public boolean isSelected() {
        return selectFlag;
    }

    public void setSelected(boolean selectFlag) {
        this.selectFlag = selectFlag;
    }

    public void drawSelected(Graphics2D gg, int matrixSize) {
        Graphics2D g2D = (Graphics2D) gg.create();

        g2D.setColor(Color.BLUE);

        g2D.setFont(new Font(null, Font.BOLD, matrixSize * 2));
        g2D.drawString(getDisplayName(), logX * matrixSize, (float) (logY + 1.6) * matrixSize);
    }

    public void drawShadow(Graphics2D gg, int matrixSize) {
        Graphics2D g2D = (Graphics2D) gg.create();

        g2D.setColor(Color.DARK_GRAY);

        g2D.setFont(new Font(null, Font.BOLD, matrixSize * 2));
        g2D.drawString(getDisplayName(), logX * matrixSize + shaX, 
                (float) (logY + 1.6) * matrixSize + shaY);
    }

    public boolean isShadow() {
        return shadowFlag;
    }

    public void setShadow(boolean shadow, int x, int y) {
        this.shadowFlag = shadow;
        this.shaX = x;
        this.shaY = y;
    }
    private MetroElement element;
    private String string;
    private int logX;
    private int logY;
    private boolean highLightFlag;
    private boolean selectFlag;
    private boolean shadowFlag;
    private int shaX;
    private int shaY;
    public static final int HEIGHT = 2;
}
